/***************************Copyright-DO-NOT-REMOVE-THIS-LINE**
 *
 * Condor Software Copyright Notice
 * Copyright (C) 1990-2004, Condor Team, Computer Sciences Department,
 * University of Wisconsin-Madison, WI.
 *
 * This source code is covered by the Condor Public License, which can
 * be found in the accompanying LICENSE.TXT file, or online at
 * www.condorproject.org.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * AND THE UNIVERSITY OF WISCONSIN-MADISON "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, OF SATISFACTORY QUALITY, AND FITNESS
 * FOR A PARTICULAR PURPOSE OR USE ARE DISCLAIMED. THE COPYRIGHT
 * HOLDERS AND CONTRIBUTORS AND THE UNIVERSITY OF WISCONSIN-MADISON
 * MAKE NO MAKE NO REPRESENTATION THAT THE SOFTWARE, MODIFICATIONS,
 * ENHANCEMENTS OR DERIVATIVE WORKS THEREOF, WILL NOT INFRINGE ANY
 * PATENT, COPYRIGHT, TRADEMARK, TRADE SECRET OR OTHER PROPRIETARY
 * RIGHT.
 *
 ****************************Copyright-DO-NOT-REMOVE-THIS-LINE**/
#include <MW.h>

#include <boost/current_function.hpp>

#include "MWFTask.h"

/* init */
MWFRenderFractalTask::MWFRenderFractalTask() {
	MWprintf(50, "Enter: %s\n", BOOST_CURRENT_FUNCTION);

	MWprintf(50, "Left: %s\n", BOOST_CURRENT_FUNCTION);
}

/* init too */
MWFRenderFractalTask::MWFRenderFractalTask(int part, const RenderingOptions& options) {
	MWprintf(50, "Enter: %s\n", BOOST_CURRENT_FUNCTION);

	this->part = part;
	this->options = options;
    this->image = NULL;

    printself(30);

    MWprintf(50, "Left: %s\n", BOOST_CURRENT_FUNCTION);
}

/* destruction */
MWFRenderFractalTask::~MWFRenderFractalTask() {
	MWprintf(50, "Enter: %s\n", BOOST_CURRENT_FUNCTION);

    if (image != NULL) {
        for (int y = 0; y < image->height; y++) {
            delete [] image->data[y];
        }

        delete [] image->data;
        delete image;
    }

	MWprintf(50, "Left: %s\n", BOOST_CURRENT_FUNCTION);
}

/* print the task to stdout */
void
MWFRenderFractalTask::printself(int level) {
	MWprintf(50, "Enter: %s\n", BOOST_CURRENT_FUNCTION);

	MWprintf(level, "part: %d\n", part);
    MWprintf(level, "rendering options: %s\n", options.toString().c_str());

    MWprintf(50, "Left: %s\n", BOOST_CURRENT_FUNCTION);
}

/* The driver packs the input data via RMC, the data which will be sent to a worker. */
void MWFRenderFractalTask::pack_work(void) {
	MWprintf(50, "Enter: %s\n", BOOST_CURRENT_FUNCTION);

    RMC->pack(&part, 1, 1);
    RMC->pack(&options.width, 1, 1);
    RMC->pack(&options.height, 1, 1);
    RMC->pack(&options.xmin, 1, 1);
    RMC->pack(&options.xmax, 1, 1);
    RMC->pack(&options.ymin, 1, 1);
    RMC->pack(&options.ymax, 1, 1);
    RMC->pack(&options.max_iterations, 1, 1);

    MWprintf(50, "Left: %s\n", BOOST_CURRENT_FUNCTION);
}

/* The worker unpacks input data via RMC, need to allocate space for data */
void MWFRenderFractalTask::unpack_work(void) {
	MWprintf(50, "Enter: %s\n", BOOST_CURRENT_FUNCTION);

    RMC->unpack(&part, 1, 1);
    RMC->unpack(&options.width, 1, 1);
    RMC->unpack(&options.height, 1, 1);
    RMC->unpack(&options.xmin, 1, 1);
    RMC->unpack(&options.xmax, 1, 1);
    RMC->unpack(&options.ymin, 1, 1);
    RMC->unpack(&options.ymax, 1, 1);
    RMC->unpack(&options.max_iterations, 1, 1);

    MWprintf(50, "Left: %s\n", BOOST_CURRENT_FUNCTION);
}

/* The worker packs result data via RMC, the result will be sent back to driver */
void MWFRenderFractalTask::pack_results(void) {
	MWprintf(50, "Enter: %s\n", BOOST_CURRENT_FUNCTION);

    for (int y = 0; y < image->height; y++) {
        RMC->pack(image->data[y],image->width,1);
    }

    MWprintf(50, "Left: %s\n", BOOST_CURRENT_FUNCTION);
}

/* The driver unpacks result data via RMC */
void MWFRenderFractalTask::unpack_results(void) {
    MWprintf(30, "Entering Task_Test::unpack_results() \n");

   image = new image_t;
   image->width = options.width;
   image->height = options.height;
   image->data = new rgb_t*[image->height];
   for (int y = 0; y < image->height; y++) {
        image->data[y] = new rgb_t[image->width];

        RMC->unpack(image->data[y],image->width,1);
    }

    MWprintf(30, "Leaving Task_Test::unpack_results() \n");
}

/* write checkpoint info per task, for each task haven't been finished */
void MWFRenderFractalTask::write_ckpt_info(FILE *fp) {
	MWprintf(50, "Enter: %s\n", BOOST_CURRENT_FUNCTION);
	MWprintf(50, "Left: %s\n", BOOST_CURRENT_FUNCTION);
}

/* Read checkpoint info, in the order written into the file */
void MWFRenderFractalTask::read_ckpt_info(FILE *fp) {
	MWprintf(50, "Enter: %s\n", BOOST_CURRENT_FUNCTION);
	MWprintf(50, "Left: %s\n", BOOST_CURRENT_FUNCTION);
}
